package com.nsfocus.network;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;

/**
 * @File: SockerDemo.java
 * @User: Liangkangkang
 * @Date: 17-4-24
 * @Time: 下午1:02
 * @Mail: liangkangkang@yahoo.com
 * @Desc:
 * @Version:
 */

public class SockerDemo {
    public static void main(String[] args) throws  Exception {
        String lineEnd = "\n";
        System.out.println("server start....");
        try {
            ServerSocket ss = new ServerSocket(8888);
            Socket socket = ss.accept();
            InputStream is = socket.getInputStream();
            // output stream
            OutputStream os = socket.getOutputStream();

            // read
            BufferedReader br = new BufferedReader(new InputStreamReader(is));

            // write
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));

            String line = null;
            while ((line = br.readLine()) != null) {
/*                SocketAddress saddr = socket.getRemoteSocketAddress();
                System.out.println(saddr + " connnect.... ");
                InetAddress addr = socket.getInetAddress();
                System.out.println("hostaddress " + addr.getHostAddress());
                System.out.println("hostname" + addr.getHostName());
                System.out.println("address " + addr.getAddress());
                System.out.println("port: " + socket.getPort());
*/
//                System.out.println("wating");
                // input stream
                System.out.println("recv data : " + line);
                String send = "hello" + line + lineEnd;
                bw.write(send);
                System.out.println("send : " + send);
                bw.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
